﻿using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Windows;

namespace MedVis
{
    class XmlReadWrite
    {
        //XML datei zu ImageSTack
        public static ImageStack readXml(string path)
        {
            ObservableCollection<MyImageObject> imageOC = new ObservableCollection<MyImageObject>();
            ImageStack iS = ImageStack.GetInstance;
            try
            {
                //pruefen, ob die Datei vorhanden ist
                if (File.Exists(path))
                {
                    //wenn ja, deserialisieren und in ImageStack speichern
                    XmlSerializer xs = new XmlSerializer(typeof(ImageStack));
                    using (StreamReader rd = new StreamReader(path))
                    {
                        iS= xs.Deserialize(rd) as ImageStack;
                    }
                }
            }
            catch (Exception d)
            {
                Console.Write(d.Message);
                throw;
            }
            return iS;
        }
        //ImageStack zu XML datei neu
        public static void writeXml(string path, String fileName, ImageStack iS)
        {
            try
            {
                //pfad erstellen, falls noch nicht vorhanden
                Directory.CreateDirectory(path);
                //Collection serialisieren und in eine XML-Datei schreiben
                XmlSerializer xs = new XmlSerializer(typeof(ImageStack));
                using (StreamWriter wr = new StreamWriter(Path.Combine(path, fileName)))
                {
                    xs.Serialize(wr, iS);
                }
            }
            catch (Exception d)
            {
                Console.Write(d.Message);
                MessageBox.Show("Es ist ein Fehler beim speichern aufgetreten", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                throw;
            }
        }
    }
}
